package com.stgl.action;

import com.alibaba.fastjson.JSONArray;
import com.stgl.service.SthdfdbService;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@EnableAutoConfiguration
@RequestMapping("/sthd")
public class sthdfdb {
    @Resource
    private SthdfdbService sthdfdbService;



    @RequestMapping("Search_sthdfdb.html")
    @ResponseBody
    public Map<String,Object>Search_sthdfdb(HttpServletRequest request, HttpServletResponse response){
        String mc=request.getParameter("mc").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("mc",mc);
        String sstate=request.getParameter("sstate").trim();
        gmap.put("sstate",sstate);

        List<Map<String,Object>>list1=sthdfdbService.Search_sthdfdb(gmap);
        Map<String,Object>rmap=new HashMap<String,Object>();
        rmap.put("data",list1);
        return rmap;
    }


    @RequestMapping("del_stkfdb.html")
    @ResponseBody
    public Map<String,Object> del_stkfdb(HttpServletRequest request, HttpServletResponse response){
        String sid=request.getParameter("sid").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sid",sid);
        gmap.put("wsid",sid);
        Map<String,Object>rmap=new HashMap<String,Object>();
        rmap=sthdfdbService.del_stkfdb(gmap);
        return rmap;
    }


    @RequestMapping("InsertStkfdb.html")
    @ResponseBody
    public Boolean InsertStkfdb(HttpServletRequest request, HttpServletResponse response){
        String mc="";
        if(request.getParameter("mc")!=null){
            mc=request.getParameter("mc").trim();
        }

        String jj="";
        if(request.getParameter("jj")!=null){
            jj=request.getParameter("jj").trim();
        }

        String fy="";
        if(request.getParameter("fy")!=null){
            fy=request.getParameter("fy").trim();
        }

        String xe="";
        if(request.getParameter("xe")!=null){
            xe=request.getParameter("xe").trim();
        }

        String sksj="";
        if(request.getParameter("sksj")!=null){
            sksj=request.getParameter("sksj");
        }

        String skdd="";
        if(request.getParameter("skdd")!=null){
            skdd=request.getParameter("skdd").trim();
        }

        String bmzlsm="";
        if(request.getParameter("bmzlsm")!=null){
            bmzlsm=request.getParameter("bmzlsm").trim();
        }

        String stksf="";
        if(request.getParameter("stksf")!=null){
            stksf=request.getParameter("stksf");
        }

        String stk_teacher="";
        if(request.getParameter("stk_teacher")!=null){
            stk_teacher=request.getParameter("stk_teacher");
        }

        List<Map<String,String>> stksflist = (List<Map<String,String>>) JSONArray.parse(stksf);
        List<Map<String,String>> stlsit = (List<Map<String,String>>) JSONArray.parse(stk_teacher);

        Map<String,Object>gmap=new HashMap<String,Object>();
        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("csid")!=null){
            gmap.put("csid",session.getAttribute("csid").toString());
        }else{
            return false;
        }
        String sid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("sid",sid);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("jlxzsj",df.format(new Date()));
        gmap.put("mc",mc);
        gmap.put("jj",jj);
        gmap.put("fy",fy);
        gmap.put("xe",xe);
        gmap.put("sksj",sksj);
        gmap.put("skdd",skdd);
        gmap.put("bmzlsm",bmzlsm);

        int i=sthdfdbService.InsertStkfdb(gmap);
        if(i>=0){
            for(int m=0;m<stksflist.size();m++){
                String sfid = UUID.randomUUID().toString().replaceAll("-", "");
                Map<String,Object>whkmap=new HashMap<String,Object>();
                whkmap.put("sfid",sfid);
                whkmap.put("sid",sid);
                whkmap.put("xmmc",stksflist.get(m).get("xmmc"));
                whkmap.put("fee",stksflist.get(m).get("fee"));
                sthdfdbService.InsertStksf(whkmap);
            }
            for(int m=0;m<stlsit.size();m++){
                String stid = UUID.randomUUID().toString().replaceAll("-", "");
                Map<String,Object>whkmap=new HashMap<String,Object>();
                whkmap.put("stid",stid);
                whkmap.put("sid",sid);
                whkmap.put("tid",stlsit.get(m).get("tid"));
                sthdfdbService.InsertStk_teacher(whkmap);
            }
            return true;
        }else{
            return false;
        }
    }




    @RequestMapping("EditStkfdb.html")
    @ResponseBody
    public Boolean EditStkfdb(HttpServletRequest request, HttpServletResponse response){
        String sid=request.getParameter("sid").trim();
        String mc="";
        if(request.getParameter("mc")!=null){
            mc=request.getParameter("mc").trim();
        }

        String jj="";
        if(request.getParameter("jj")!=null){
            jj=request.getParameter("jj").trim();
        }

        String fy="";
        if(request.getParameter("fy")!=null){
            fy=request.getParameter("fy").trim();
        }

        String xe="";
        if(request.getParameter("xe")!=null){
            xe=request.getParameter("xe").trim();
        }

        String sksj="";
        if(request.getParameter("sksj")!=null){
            sksj=request.getParameter("sksj");
        }

        String skdd="";
        if(request.getParameter("skdd")!=null){
            skdd=request.getParameter("skdd").trim();
        }

        String bmzlsm="";
        if(request.getParameter("bmzlsm")!=null){
            bmzlsm=request.getParameter("bmzlsm").trim();
        }

        String stksf="";
        if(request.getParameter("stksf")!=null){
            stksf=request.getParameter("stksf");
        }

        String stk_teacher="";
        if(request.getParameter("stk_teacher")!=null){
            stk_teacher=request.getParameter("stk_teacher");
        }

        List<Map<String,String>> stksflist = (List<Map<String,String>>) JSONArray.parse(stksf);
        List<Map<String,String>> stlsit = (List<Map<String,String>>) JSONArray.parse(stk_teacher);

        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sid",sid);
        gmap.put("mc",mc);
        gmap.put("jj",jj);
        gmap.put("fy",fy);
        gmap.put("xe",xe);
        gmap.put("sksj",sksj);
        gmap.put("skdd",skdd);
        gmap.put("bmzlsm",bmzlsm);
        int i=sthdfdbService.EditStkfdb(gmap);
        if(i>=0){
            for(int m=0;m<stksflist.size();m++){
                String sfid = UUID.randomUUID().toString().replaceAll("-", "");
                Map<String,Object>whkmap=new HashMap<String,Object>();
                whkmap.put("sfid",sfid);
                whkmap.put("sid",sid);
                whkmap.put("xmmc",stksflist.get(m).get("xmmc"));
                whkmap.put("fee",stksflist.get(m).get("fee"));
                sthdfdbService.InsertStksf(whkmap);
            }
            for(int m=0;m<stlsit.size();m++){
                String stid = UUID.randomUUID().toString().replaceAll("-", "");
                Map<String,Object>whkmap=new HashMap<String,Object>();
                whkmap.put("stid",stid);
                whkmap.put("sid",sid);
                whkmap.put("tid",stlsit.get(m).get("tid"));
                sthdfdbService.InsertStk_teacher(whkmap);
            }
            return true;
        }else{
            return false;
        }
    }



    @RequestMapping("delHistoryStkfdb.html")
    @ResponseBody
    public boolean delHistoryStkfdb(HttpServletRequest request, HttpServletResponse response){
        String sids=request.getParameter("sids").trim();
        List<Map<String,String>> listw = (List<Map<String,String>>) JSONArray.parse(sids);
        Map<String,Object>gmap=new HashMap<String,Object>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String jlscsj=df.format(new Date());
        for(int i=0;i<listw.size();i++){
            gmap.put("jlscsj",jlscsj);
            String sid=listw.get(i).get("sid").toString();
            gmap.put("sid",sid);
            sthdfdbService.delHistoryStkfdb(gmap);
        }
        return true;
    }




    //恢复历史报名
    @RequestMapping("resHistoryStkfdb.html")
    @ResponseBody
    public boolean resHistoryStkfdb(HttpServletRequest request, HttpServletResponse response){
        String sids=request.getParameter("sids").trim();
        List<Map<String,String>> listw = (List<Map<String,String>>) JSONArray.parse(sids);
        Map<String,Object>gmap=new HashMap<String,Object>();
        for(int i=0;i<listw.size();i++){
            String sid=listw.get(i).get("sid").toString();
            gmap.put("sid",sid);
            sthdfdbService.resHistoryStkfdb(gmap);
        }
        return true;
    }

}
